package cn.batis.uitil;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @Author: DELL
 * @Since: 2024/11/4 15:44
 * @Description:
 */
public class MybatiUtil {
    private static SqlSessionFactory ssf;
    static {
        try{
            InputStream is= Resources.getResourceAsStream("mybatis-config.xml");
            ssf=new SqlSessionFactoryBuilder().build(is);
        }catch (IOException e){
            throw new RuntimeException("初始化失败"+e);
        }
    }
    public static SqlSession createSqlSession(){
        return ssf.openSession(false);
    }
    public static void closeSession(SqlSession sqlSession){
        if (sqlSession!=null){
            sqlSession.close();
        }
    }
}
